﻿using AutoMapper;
using MediatR;
using T.S.BaseData.Api.AppClinet.Commend.SlaugHter;
using T.S.BaseData.Api.Dto.Slaughter;
using T.S.BaseData.Domain.Entity;
using T.S.BaseData.ErrorCode;
using T.S.BaseData.Infrastructure.Base;

namespace T.S.BaseData.Api.AppClinet.CommendHand.SlaugHter
{
    public class SlaughterGetCommandHandle : IRequestHandler<SlaughterGetCommand, ApiResult<List<GetSlaughterDto>>>
    {

        private readonly IBaseRepository<Slaughter> slaugRepo;
        private readonly IMapper mapper;

        public SlaughterGetCommandHandle(IBaseRepository<Slaughter> slaugRepo, 
            IMapper mapper)
        {
            this.slaugRepo = slaugRepo;
            this.mapper = mapper;
        }

        public Task<ApiResult<List<GetSlaughterDto>>> Handle(SlaughterGetCommand request, CancellationToken cancellationToken)
        {

            ApiResult<List<GetSlaughterDto>> result = new ApiResult<List<GetSlaughterDto>>();

            var slaug = slaugRepo.GetAll().Where(x=>x.GoodsId == request.GoodsId);

            var model = mapper.Map<List<GetSlaughterDto>>(slaug);

            result.Code = ApiEnums.Success;
            result.Data = model;
            result.Meg = "显示成功";


            return Task.FromResult(result);
        }
    }
}
